package com.record.ccod;

import com.elite.groovy.kh.KHDBConfig;
import com.elite.groovy.kh.KHUtil;
import cn.com.tpqn.sessionDetailRecord;
import com.record.utils.RecUtils;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.servlet.http.HttpServletResponse;
import java.math.BigInteger;

public class CCODUtils {
    private static final Log log = LogFactory.getLog(CCODUtils.class);
    private static JSONObject rec_Config = new JSONObject();
    private static String amrconvertcmd = "";

    // CCOD(青牛)录音平台获取
    public static String doCCODRecord(JdbcTemplate jt, HttpServletResponse response, String entId, String recData, String multimode) {
        log.debug("CCOD(青牛)录音平台获取 recordPath:" + recData);
        String res_RecordStr = "";

        try {
            // 初始化参数
            rec_Config = KHUtil.SFIsNull(rec_Config.toString()) ? JSONObject.fromObject(KHDBConfig.getParamConfig(jt, "REC_CFG-" + entId)) : rec_Config;
            amrconvertcmd = KHUtil.GetSFJV(rec_Config, "amrconvertcmd");

            String[] recArray = recData.split("\\|");
            if (recArray.length == 4) {
                // 解析获取录音SessionID(DB存储时16进制,调用青牛需要转10进制)
                String sessionId16 = recArray[1];
                String sessionId10 = new BigInteger(sessionId16, 16).toString(10);

                res_RecordStr = sessionDetailRecord.main(jt, sessionId10, multimode);
                log.debug("CCOD(青牛)录音平台 sessionId(16):" + sessionId16 + " sessionId(10):" + sessionId10 + " res_RecordStr:" + res_RecordStr);

                // multimode非空是DYN录音调听或下载,按DYN模式返回录音信息
                if (res_RecordStr.startsWith("http://")) {

                    if (!KHUtil.isNull(amrconvertcmd)) {
                        RecUtils.ConvertAmrAudio(jt, response, entId, res_RecordStr);
                        return "";
                    }else{
                        response.sendRedirect(res_RecordStr);
                        return "";
                    }
                }

            } else {
                log.warn("CCOD(青牛)录音平台获取 recData is invalid");
            }
        } catch (Exception e) {
            log.error("CCOD(青牛)录音平台获取 异常", e);
        }
        log.debug("CCOD(青牛)录音平台获取 res_Data:" + res_RecordStr);

        return res_RecordStr;

    }
}
